const app = getApp()
//const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const defaultAvatarUrl = '../../images/default.jpg'

Page({
	data: {
		avatarUrl: defaultAvatarUrl,
		nickName: ""
	},
	onLoad() {
		wx.hideHomeButton()
		this.setData({
			avatarUrl: defaultAvatarUrl,
			nickName:""
		})
	},
	onChooseAvatar(e) {
		console.log(e.detail)
		this.setData({
			avatarUrl: e.detail.avatarUrl
		})
	},
	onChooseNickName(e) {
		console.log(e.detail)
		this.data.nickName = e.detail.value
	},
	onNickConfirm(e) {
		this.data.nickName = e.detail.value
	},
	checkNickNameValid(username) {
		console.log(username)
		var reg = /^[A-Za-z0-9-_\u4e00-\u9fa5]{4,20}$/;
		if (!reg.test(username)) {
			return false
		} else {
			return true
		}
	},
	onConfirm(e) {
		if (this.data.avatarUrl == defaultAvatarUrl) {
			wx.showToast({
				title: '请设置头像',
			})
			return
		}
		if (this.data.nickName == "") {
			wx.showToast({
				title: '请设置昵称',
			})
			return
		}
		if (!this.checkNickNameValid(this.data.nickName)) {
			wx.showToast({
				title: '昵称长度为4~20位',
				icon:'none'
			})
			return
		}
		wx.showLoading({
			title: '保存中',
		})
		if (!app.getopenid()) {
			wx.showToast({
				title: '登录已过期, 请重新登录',
				duration: 1000,
				icon:'none'
			})
			setTimeout(function() {
				wx.redirectTo({
				  url: '../index/index',
				})
			}, 1000)
		} else {
			var that = this
			let FileSystemManager = wx.getFileSystemManager()
			FileSystemManager.getFileInfo({
				"filePath": that.data.avatarUrl,
				success(res) {
					console.log('FileInfo-res', res)
					if (res.size > 1024 * 1024 * 1) {
						wx.showToast({
							title: '所选图片过大，请选择1M以内的图片',
							icon: 'none'
						})
					} else if (res.size == 0) {
						wx.showToast({
							title: '所选图片不存在, 请重新选择',
							icon: 'none'
						})
					} else {
						that.onSyncInfo()
					}
				},
				fail(err) {
					console.log('FileInfo-err', err)
					wx.showToast({
						title: '选择头像文件错误',
						icon: 'none'
					})
				},
			})
		}
	},
	async onSyncInfo() {
		let res = await this.updateUser()
		const rspData = JSON.parse(res)
		console.log(rspData.avatarUrl)
		var ndata = {
			"avatarUrl": rspData.avatarUrl,
			"nickName": this.data.nickName,
		}
		app.setnickavatar(ndata)
		wx.hideLoading()
		wx.showToast({
			title: '保存成功',
			duration: 1000
		})
		setTimeout(function() {
			wx.reLaunch({
				url: '../index/index',
			  })
		}, 1000)
	},
	updateUser() {
		var that = this
		return new Promise((resolve,reject)=>{
			wx.uploadFile({
				filePath: that.data.avatarUrl,
				name: 'avatarPath',
				url: app.globalData.BASE_URL + "/mallUpdateUser",
				formData: {
					uId: app.getopenid(),
					nickName: that.data.nickName
				},
				success: function (res) {
					if (res.statusCode != 200) {
						wx.showToast({
							title: '服务器忙，请稍后再试',
							icon:'none'
						})
						reject({ error: '服务器忙，请稍后重试', code: 500 })
					}
					resolve(res.data)
				},
				fail: function (res) {
					console.log(res)
					wx.showToast({
						title: '服务器忙，请稍后再试',
						icon:'none'
					})
					// fail调用接口失败
					reject({ error: '网络错误', code: 0 })
				}
			})
		})
	},
})
